" 插件
call plug#begin('~/.vim/plugged')
Plug 'itchyny/lightline.vim'
Plug 'itchyny/vim-cursorword'
Plug 'yianwillis/vimcdoc'
call plug#end()
" 说明
" https://github.com/junegunn/vim-plug
"
" unix
" curl -fLo ~/.vim/autoload/plug.vim --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
"
" win
" iwr -useb https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim |  ni $HOME/vimfiles/autoload/plug.vim -Force
" 
" 命令
" :PlugStatus  #查看插件状态
" :PlugInstall #安装之前在配置文件中声明的插件
" 常用操作如下
" :PlugUpdate   #更新插件
" :PlugDiff     #查看插件的变化状态，简单地回滚有问题的插件。
" :PlugClean    #删除插件

" theme
colorscheme wombat256grf


" 设置外观 -------------------------------------
set number                     				 "显示行号 
set showtabline=0              				 "隐藏顶部标签栏"
set guioptions-=r              				 "隐藏右侧滚动条" 
set guioptions-=L              				 "隐藏左侧滚动条"
set guioptions-=b              				 "隐藏底部滚动条"
set cursorline                 				 "突出显示当前行"
set cursorcolumn               				 "突出显示当前列"
set langmenu=zh_CN.UTF-8       				 "显示中文菜单
set showcmd                    				 "显示命令

" 变成辅助 -------------------------------------
syntax on                          				 "开启语法高亮
set nowrap                     				 "设置代码不折行"
set fileformat=unix            				 "设置以unix的格式保存文件"
set cindent                    				 "设置C样式的缩进格式"
set backspace+=indent,eol,start				 "set backspace&可以对其重置
set showmatch                  				 "显示匹配的括号"
set scrolloff=5                				 "距离顶部和底部5行"
set laststatus=2               				 "命令行为两行"
set tabstop=4                  				 "一个 tab 显示出来是多少个空格，默认 8
set expandtab                          " 将\t 设为 space
set shiftwidth=4               				 "每一级缩进是多少个空格
set expandtab										" table 变为 space
" 光标回到上次退出位置
au BufReadPost * if line("'\"") > 0 | if line("'\"") <= line("$") | exe("norm '\"") | else |exe "norm $"| endif | endif

" #其他杂项 -------------------------------------
set mouse=a                    				 "启用鼠标"
set mouse=v                    				 " 粘贴
set mouse=n                    				 " 
" set mouse=r                  				 " 粘贴 
set selection=exclusive
set selectmode=mouse,key
set matchtime=5
set ignorecase                 				 "忽略大小写"
" set incsearc
set hlsearch                   				 "高亮搜索项"
set noexpandtab                				 "不允许扩展table"
set whichwrap+=<,>,h,l
set autoread


" 设置快捷键
" imap   <ESC>lC
imap <C-k>  <ESC>lC
" imap   <ESC>:|,$ d
imap  <ESC>A
imap  <ESC>I



" China	Oset encoding=UTF-8
"set encoding=utf-8				" 
set langmenu=zh_CN.UTF-8
language message zh_CN.UTF-8
set fileencoding=utf-8
set termencoding=utf-8
set helplang=cn


set autowriteall   				 " 自动保存





let out = "~/code/out"
let cxxflags = "-std=gun++17"



" 设置编译器和解释器
function SetCompilation()
	" 获取文件类型 
	let ftype = &filetype
	" 根据类型设置编译方式
	if ftype == 'c'
		return "gcc"
	elseif ftype == 'cpp'
		return "g++ " . g:cxxflags
	elseif ftype == 'java'
		return "java"
	elseif ftype == 'python'
		return "python3"
	elseif ftype == 'vim'
		return "vim"
	endif
endfunction



" <F5> 编译 运行
" autocmd filetype cpp nnoremap <F4> :!g++ -std=c++11 % -o %<.out<CR>
imap <F5> <ESC>:call CompRun()<CR>
map <F5> :call CompRun()<CR>

imap <F6> <ESC>:call CompRunTest()<CR>
map <F6> :call CompRunTest()<CR>

function CompRun()
	exec "w"
	let cmd = "!clear && "
	
	" 设置 编译器 解释器
	let comp = SetCompilation() . " "
	let cmd = cmd . comp . expand("%")
 
	" c/c++ 规定输出文件
 	let ftype = &filetype
 	if ftype == 'cpp' || ftype == 'c'
 		let cmd = cmd . " -o " . g:out . "/" . expand("%:t") . " "
		let run = "time " . g:out . "/" . expand("%:t")
		let cmd = cmd . " && " . run
 	endif
 
	" 执行
	exec cmd
 	
endfunction

" a.in 为测试文件
function CompRunTest()
	exec "w"
	let cmd = "!clear && "
	
	" 设置 编译器 解释器
	let comp = SetCompilation() . " "
	let cmd = cmd . comp . expand("%")
 
	" c/c++ 规定输出文件
 	let ftype = &filetype
 	if ftype == 'cpp' || ftype == 'c'
 		let cmd = cmd . " -o " . g:out . "/" . expand("%:t") . " "
		let run = "time " . g:out . "/" . expand("%:t")
		" let run = "echo ". g:out . "/" . expand("%:t")
		let cmmd = cmd . " && " . run . "< a.in"
 	endif
 
	" 执行
    " exec "echo " . cmmd
	exec cmmd . " && echo "
    " exec "echo '\r'"
	" exec "cat ./a.txt"
 	
endfunction


" 新建文件
autocmd BufNewFile *.[ch],*.hpp,*.cpp,Makefile,*.mk,*.sh exec ":call SetTitle()"
" 加入注释
func SetComment()
        call setline(1,"/*================================================================")
        " call append(line("."),   "*   Copyright (C) ".strftime("%Y")." baichao All rights reserved.")
        call append(line(".")+0, "*   ")
        call append(line(".")+1, "*   文件名称：".expand("%:t"))
        call append(line(".")+2, "*   创 建 者：".expand("$USERNAME"))
        call append(line(".")+3, "*   创建日期：".strftime("%Y年%m月%d日"))
        call append(line(".")+4, "*   描    述：")
        call append(line(".")+5, "*")
        call append(line(".")+6, "================================================================*/")
        call append(line(".")+7, "")
        call append(line(".")+8, "")
endfunc
" 加入shell,Makefile注释
func SetComment_sh()
        call setline(3, "#================================================================")
        " call setline(4, "#   Copyright (C) ".strftime("%Y")." Sangfor Ltd. All rights reserved.")
        call setline(4, "#   ")
        call setline(5, "#   文件名称：".expand("%:t"))
        call setline(6, "#   创 建 者：".expand("$USERNAME"))
        call setline(7, "#   创建日期：".strftime("%Y年%m月%d日"))
        call setline(8, "#   描    述：")
        call setline(9, "#")
        call setline(10, "#================================================================")
        call setline(11, "")
        call setline(12, "")
endfunc
" 定义函数SetTitle，自动插入文件头
func SetTitle()
        if &filetype == 'make'
                call setline(1,"")
                call setline(2,"")
                call SetComment_sh()
        elseif &filetype == 'sh'
                call setline(1,"#!/bin/sh")
                call setline(2,"")
                call SetComment_sh()
 
        else
                call SetComment()
                if expand("%:e") == 'hpp'
                        call append(line(".")+9, "")
                        call append(line(".")+10, "#ifndef _".toupper(expand("%:t:r"))."_H")
                        call append(line(".")+11, "#define _".toupper(expand("%:t:r"))."_H")
                        call append(line(".")+12, "#ifdef __cplusplus")
                        call append(line(".")+13, "extern \"C\"")
                        call append(line(".")+14, "{")
                        call append(line(".")+15, "#endif")
                        call append(line(".")+16, "")
												call append(line(".")+17, "#ifdef __cplusplus")
                        call append(line(".")+18, "}")
                        call append(line(".")+19, "#endif")
                        call append(line(".")+20, "#endif //".toupper(expand("%:t:r"))."_H")
 
                elseif expand("%:e") == 'h'
                        call append(line(".")+10, "#pragma once")
                elseif &filetype == 'c'
                        call append(line(".")+10,"#include \"".expand("%:t:r").".h\"")
                elseif &filetype == 'cpp'
                        call append(line(".")+10, "#include \"".expand("%:t:r").".h\"")
                endif
        endif
		" 光标自动扫最后
		exec "%"
    " autocmd BufNewFile * normal G
endfunc




" hi Pmenu ctermfg=223 ctermbg=239 guifg=#ebdbb2 guibg=#504945
" hi PmenuSbar ctermbg=239 guibg=#504945
" hi PmenuSel cterm=bold ctermfg=239 ctermbg=109 gui=bold guifg=#504945 guibg=#83a598


